package zongbao.interrceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class PrivilegeInterceptor implements HandlerInterceptor
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        获取请求的路径
        String url=request.getRequestURI();
        //对用户登录的相关请求，放行
        if (url.contains("/user/login")||url.contains("/adim/login")||url.contains("/user/inadd")||url.contains("/user/add")||url.contains("/user/wm")
        ||url.contains("/user/zhaohui")||url.contains("/user/dao")||url.contains("/adim/inadd")||url.contains("/adim/add")||url.contains("/adim/wm")
                ||url.contains("/adim/zhaohui")||url.contains("/adim/dao")){
            return true;
        }
        HttpSession session=request.getSession();
        //如果用户是已登录状态，放行
        if (session.getAttribute("USER_SESSION")!=null){
            return true;
        }
        //其他情况都直接跳转到登录页面
        request.setAttribute("msg","没登录，先登录");
        request.getRequestDispatcher("jsp/login.jsp").forward(request,response);;
        return false;
//return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
